<?php

namespace app\admin\model\agent;

use app\common\model\Agent;
use app\common\model\order\Order;
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
use think\Db;
use think\Model;

class Bill extends Model
{
    // 表名
    protected $name = 'agent_bill';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'type_text',
        'status_text'
    ];
    

    
    public function getTypeList()
    {
        return ['1' => __('充值'),'2' => __('订单支出'),'3' => __('调拨'),'4' => __('添加保证金'),'5'=>__('作废')];
    }

    public function getStatusList()
    {
        return ['-1' => __('Status -1'),'0' => __('Status 0'),'1' => __('Status 1'),'2' => __('Status 2'),'3' => __('Status 3'),'4'=>'作废'];
    }     


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * 财务审核打款详细
     * @param $price
     * @param $order
     * @return bool
     */
    public function money($price,$order)
    {
        //付款金额 必须大于等于 订单金额减去已付款金额
        if ($price >= ($order->order_price - $order->check_order))
        {
            //订单总价减去已付款金额等于应付款金额
            $price = $price - ($order->order_price - $order->check_order);
            Db::transaction(function() use ($price,$order){
                if($price > 0){
                    //生成充值金额详情
                    Bill::create([
                        'order_id'=>$order->id,
                        'agent_id'=>$order->userid,
                        'type'=>1,
                        'note'=>'订单：'.$order->nb.'剩余金额充值',
                        'amount'=>$price,
                        'status'=>3
                    ]);
                    $agent = Agent::get($order->userid);
                    $agent->deduction_amount += $price;
                    $agent->save();
                }
                //生成付款金额详情
                Bill::create([
                    'order_id'=>$order->id,
                    'agent_id'=>$order->userid,
                    'type'=>2,
                    'note'=>'订单：'.$order->nb.'付款',
                    'amount'=>($order->order_price - $order->check_order),
                    'status'=>3
                ]);
            });

            return true;
        }
        return false;
    }

    /**
     * 驳回金额回退
     * @param $orderId
     */
    public function rejected($orderId)
    {
        Db::transaction(function() use ($orderId){
            $order = Order::get($orderId);
            //订单金额退回账户
            $agent = Agent::get($order->userid);
            //未付款  带确认
            if($order->pay == 3 || $order->pay == 4)
            {
                $pay = 0;
            }
            //未结清
            if($order->pay == 2)
            {
                $pay = $order->check_order;//返回订单的已付款金额
            }
            //已付款
            if($order->pay == 1)
            {
                $pay = $order->order_price;//返回订单的总价
            }
            $agent->deduction_amount += $pay;
            $order->updatetime = time();
            $agent->save();
            if($order->pay == 1 || $order->pay == 2)
            {
                //生成充值金额详情
                Bill::create([
                    'order_id'=>$order->id,
                    'agent_id'=>$order->userid,
                    'type'=>1,
                    'note'=>'订单：'.$order->nb.'驳回金额回退',
                    'amount'=>$pay,
                    'status'=>3
                ]);
            }

            $order->pay = 3;
            $order->check_order = 0;
            $order->updatetime = time();
            $order->save();//订单已付款金额为0
        });
    }

}
